home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 2 / Macwelt DVD 2.cdr / System / Internet-Utilities / macosx / News Mac 1.1.dmg / IOUtil.class (.txt) < prev    next >
Encoding:
Java Class File  |  2002-04-08  |  3.8 KB  |  105 lines

  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11. import java.io.ObjectInputStream;
  12. import java.io.ObjectOutputStream;
  13. import java.io.Serializable;
  14. import java.io.Writer;
  15. import java.util.Vector;
  16.  
  17. public class IOUtil implements Serializable {
  18.    public static void saveObject(String fileName, Object theObject) throws IOException {
  19.       ObjectOutputStream writingPipe = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
  20.       writingPipe.writeObject(theObject);
  21.       writingPipe.close();
  22.    }
  23.  
  24.    public static Object openObject(String fileName) throws IOException {
  25.       Object theData = new Object();
  26.       ObjectInputStream readingPipe = new ObjectInputStream(new BufferedInputStream(new FileInputStream(fileName)));
  27.  
  28.       try {
  29.          theData = readingPipe.readObject();
  30.          readingPipe.close();
  31.       } catch (ClassNotFoundException var4) {
  32.          System.out.println("Class not found!");
  33.       }
  34.  
  35.       return theData;
  36.    }
  37.  
  38.    public static Vector openTextFile(String fileName) throws IOException {
  39.       Vector temp = new Vector(100, 100);
  40.  
  41.       try {
  42.          boolean exitLoop = false;
  43.          FileInputStream theData = new FileInputStream(fileName);
  44.          BufferedReader readingPipe = new BufferedReader(new InputStreamReader(theData));
  45.  
  46.          do {
  47.             String theString = readingPipe.readLine();
  48.             if (theString != null) {
  49.                temp.addElement(theString);
  50.             } else {
  51.                exitLoop = true;
  52.             }
  53.          } while(!exitLoop);
  54.  
  55.          readingPipe.close();
  56.          return temp;
  57.       } catch (Exception var6) {
  58.          throw new IOException();
  59.       }
  60.    }
  61.  
  62.    public static void saveTextFile(String theFileName, String theText, boolean append) throws IOException {
  63.       BufferedWriter out = new BufferedWriter(new FileWriter(theFileName, append));
  64.       ((Writer)out).write(theText);
  65.       out.close();
  66.    }
  67.  
  68.    public static boolean fileExists(String theFileName) {
  69.       File theFile = new File(theFileName);
  70.       return theFile.exists();
  71.    }
  72.  
  73.    public static boolean deleteFile(String theFileName) throws SecurityException {
  74.       File theFile = new File(theFileName);
  75.       return theFile.delete();
  76.    }
  77.  
  78.    public static String[] dirListing(String path) throws IOException {
  79.       try {
  80.          File temp = new File(path);
  81.          return temp.list();
  82.       } catch (Exception var2) {
  83.          throw new IOException();
  84.       }
  85.    }
  86.  
  87.    public static boolean makeDirectory(String dir) throws Exception {
  88.       try {
  89.          File temp = new File(dir);
  90.          return temp.mkdirs();
  91.       } catch (Exception var2) {
  92.          throw new Exception(((Throwable)var2).toString());
  93.       }
  94.    }
  95.  
  96.    public static void main(String[] args) throws IOException {
  97.       try {
  98.          openTextFile("data/email-list.txt");
  99.       } catch (Exception var2) {
  100.          System.out.println("urf! " + var2);
  101.       }
  102.  
  103.    }
  104. }
  105.